<?php

/**
 * Class ExcelService 表格导出
 */
namespace app\common\service;

use app\common\model\user\UserCoinModel;

class CmsDataCountDayTypeConfig
{
    // 用户持仓点击统计
    const user_read_avg_list = 'user_read_avg_list';


    /**
     * 获取单日人均点击数统计
     * @param string $time
     * @return array
     */
    public static function user_read_avg_list( $time = "" )
    {
        $time  = $time ? $time : date('Y-m-d') ;
        $start_time = date('Y-m-d',strtotime( $time ) ) ;
        $end_time = date('Y-m-d 23:59:59',strtotime( $time ) ) ;

        $modelUserSta = new UserStatisticsMongo();
        $apiModel = new ApiLogMongo();
        $modelUserCoin = UserCoinModel::getInstance();
        $modelUserSta->where_between("create_time", strtotime( $start_time ), strtotime( $end_time ) );

        $modelUserSta->where_eq("is_register", 1);
        $modelUserSta->field( array( 'create_time', 'is_asset', 'user_id' ) );
        $res = $modelUserSta->get();

        // 返回数据
        $data_list = array();

        $data = array( );

        if ( $res ) {
            $user_data_list = array();
            $active_user_id_list = array();
            $active_asset_user_list = array();
            $active_noasset_user_list = array();

            // 当日活跃用户
            $active_user_id_list = $apiModel->get_active_user_id_list_by_date( $start_time );

            foreach ( $res as $v ) {
                if ( !isset($user_data_list[$v['is_asset']][$v['user_id']]) ) {
                    $user_data_list[$v['is_asset']][$v['user_id']] = 0;
                }
                $user_data_list[$v['is_asset']][$v['user_id']]++;


                if ( $active_user_id_list ) {
                    $date_time = date("Y-m-d H:i:s", $v["create_time"]);
                    $where = array(
                        'user_id' => array('in', $active_user_id_list),
                        'create_time' => array( 'elt', $date_time ),
                    );
                    $asset_user_id_list = $modelUserCoin->search_id_list_no_page($where, 'user_id');
                    $active_asset_user_list = array_merge($active_asset_user_list, $asset_user_id_list);
                    $noasset_user_id_list = array_diff($active_user_id_list, $asset_user_id_list);
                    $active_noasset_user_list = array_merge($active_noasset_user_list, $noasset_user_id_list);
                }

                // 当日(并且是当前时间点上)
                if ( $v['is_asset'] ) {
                    // 持仓用户
                    $active_asset_user_list[] = $v['user_id'];
                }else {
                    // 未持仓用户
                    $active_noasset_user_list[] = $v['user_id'];
                }
            }

            $data = array(
                "date" => $start_time,
                // 持仓用户总数
                "asset_user_count"=>0,
                // 持仓用户点击数量
                "asset_count" => 0,
                // 持仓用户人均点击次数
                "asset_avg" => '',
                // 非持仓用户总数
                "noasset_user_count" => 0,
                // 非持仓用户点击数量
                "noasset_count" => 0,
                // 非持仓用户人均点击次数
                "noasset_avg" => '',
            );


            foreach ( $user_data_list as $is_asset=>$_user_article ) {
                if ( $is_asset ) {
                    // 持仓用户总数
                    //$data_list[$_date]['asset_user_count'] = count($_user_article);
                    $data['asset_user_count'] = count(array_unique($active_asset_user_list));
                    // 持仓用户点击数量
                    $data['asset_count'] = array_sum($_user_article);
                    // 持仓用户人均点击次数
                    $data['asset_avg'] = bcdiv($data['asset_count'], $data['asset_user_count'], 2);
                }else {
                    // 非持仓用户总数
                    //$data_list[$_date]['noasset_user_count'] = count($_user_article);
                    $data['noasset_user_count'] = count(array_unique($active_noasset_user_list));
                    // 非持仓用户点击数量
                    $data['noasset_count'] = array_sum($_user_article);
                    // 非持仓用户人均点击次数
                    $data['noasset_avg'] = bcdiv($data['noasset_count'], $data['noasset_user_count'], 2);
                }
            }
        }

        return $data;
    }

}